package com.zhz.company.config.mybtatis;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.zhz.common.tool.mybatisplus.base.dal.utils.JsonUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * YgpJsonUtils 特殊处理时间格式
 * @author zhouhengzhe
 */
@MappedTypes({Object.class})
@MappedJdbcTypes({JdbcType.JAVA_OBJECT})
public class JsonTypeHandler extends AbstractJsonTypeHandler<Object> {

    private static final Logger LOGGER = LoggerFactory.getLogger(JacksonTypeHandler.class);

    private final Class<?> type;
    public JsonTypeHandler(Class<?> type) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("YGPJsonTypeHandler(" + type + ")");
        }

        Assert.notNull(type, "Type argument cannot be null");
        this.type = type;
    }

    @Override
    protected Object parse(String json) {
        return JsonUtils.toDTO(json, type);
    }

    @Override
    protected String toJson(Object obj) {
        return JsonUtils.toJson(obj);
    }
}
